{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "\n",
    "with open('/home/husein/ssd3/alpaca/translated-alpaca_data_cleaned.json') as fopen:\n",
    "    alpaca = json.load(fopen)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "\n",
    "malay_respond = [\n",
    "    ' respond in malay',\n",
    "    ' reply in malay',\n",
    "    ' reply bahasa melayu',\n",
    "    ' ckp bahasa melayu',\n",
    "    ''\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "template = {\n",
    "    \"description\": \"Template used by Alpaca-LoRA.\",\n",
    "    \"prompt_input\": \"Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.\\n\\n### Instruction:\\n{instruction}\\n\\n### Input:\\n{input}\\n\\n### Response:\\n\",\n",
    "    \"prompt_no_input\": \"Below is an instruction that describes a task. Write a response that appropriately completes the request.\\n\\n### Instruction:\\n{instruction}\\n\\n### Response:\\n\",\n",
    "    \"response_split\": \"### Response:\"    \n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'instruction': 'Apakah nama produk bagi iPhone 11?',\n",
       " 'input': '',\n",
       " 'output': 'Nama produk untuk iPhone 11 adalah \"iPhone 11\".'}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "alpaca[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = []\n",
    "for d in alpaca:\n",
    "    \n",
    "    if 'Terjemahkan teks' in d['instruction']:\n",
    "        continue\n",
    "        \n",
    "    if len(d['input']):\n",
    "        res = template['prompt_input'].format(instruction=d['instruction'], input=d['input'])\n",
    "    else:\n",
    "        res = template['prompt_no_input'].format(instruction=d['instruction'])\n",
    "\n",
    "    a = d['output']\n",
    "    res = f\"{res}{a}\"\n",
    "    data.append(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Below is an instruction that describes a task. Write a response that appropriately completes the request.\\n\\n### Instruction:\\n\"Namakan dua watak dari filem The Little Mermaid.\"\\n\\n### Response:\\nDua watak dari filem The Little Mermaid ialah Ariel dan Putera Eric.',\n",
       " 'Below is an instruction that describes a task. Write a response that appropriately completes the request.\\n\\n### Instruction:\\nApakah nama produk bagi iPhone 11?\\n\\n### Response:\\nNama produk untuk iPhone 11 adalah \"iPhone 11\".',\n",
       " 'Below is an instruction that describes a task. Write a response that appropriately completes the request.\\n\\n### Instruction:\\n\"Berapa lama dahulu Pertubuhan Bangsa-Bangsa Bersatu ditubuhkan?\"\\n\\n### Response:\\nBangsa-bangsa Bersatu secara rasmi ditubuhkan pada 24 Oktober 1945, iaitu 76 tahun yang lalu.',\n",
       " 'Below is an instruction that describes a task. Write a response that appropriately completes the request.\\n\\n### Instruction:\\nCipta tajuk menarik dan original untuk novel seram mengenai keluarga yang dilaknat oleh makhluk ber mata merah.\\n\\n### Response:\\n\"Kutukan Mata Merah: Hantuan Keluarga Harrow\"',\n",
       " 'Below is an instruction that describes a task. Write a response that appropriately completes the request.\\n\\n### Instruction:\\nCari lapangan terbang yang terdekat dengan Los Angeles.\\n\\n### Response:\\nLapangan terbang terdekat dengan Los Angeles ialah Lapangan Terbang Antarabangsa Los Angeles (LAX), terletak kira-kira 18 batu dari barat daya pusat bandar Los Angeles.',\n",
       " 'Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.\\n\\n### Instruction:\\nTulis semula ayat berikut untuk mengelakkan penggunaan klise \"tanpa perlu dikatakan\".\\n\\n### Input:\\nTidak dapat dinafikan bahawa senaman adalah penting untuk kesihatan anda.\\n\\n### Response:\\nSenaman adalah penting untuk kesihatan anda tanpa ragu.',\n",
       " 'Below is an instruction that describes a task. Write a response that appropriately completes the request.\\n\\n### Instruction:\\nLengkapkan ayat. Cara terbaik untuk menjimatkan wang adalah dengan _____.\\n\\n### Response:\\nCara terbaik untuk menjimatkan wang adalah dengan membuat bajet, mengesan perbelanjaan anda dan berpegang kepada matlamat kewangan anda sambil sentiasa mencari cara untuk mengurangkan perbelanjaan dan meningkatkan pendapatan anda.',\n",
       " 'Below is an instruction that describes a task. Write a response that appropriately completes the request.\\n\\n### Instruction:\\n\"Senaraikan dua kemewahan yang boleh dibeli dengan $1000.\"\\n\\n### Response:\\n1. Beg tangan reka bentuk berkualiti tinggi atau jam tangan mewah\\n2. Pakej hari spa mewah untuk relaksasi dan pembaharuan diri',\n",
       " 'Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.\\n\\n### Instruction:\\n\"Sebutkan tiga ciri yang menggambarkan watak dalam cerita berikut.\"\\n\\n### Input:\\n\"John adalah seorang remaja yang pemalu dan introvert yang suka bermain permainan video pada masa lapangnya.\"\\n\\n### Response:\\n1. Pemalu.\\n2. Introvert.\\n3. Pemain permainan video.',\n",
       " 'Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.\\n\\n### Instruction:\\n\"Sunting ayat ini untuk menjadikannya betul dari segi tatabahasa: Dia adalah seorang ahli matematik.\"\\n\\n### Input:\\nThe correct translation in standard Malay language would be: \\'Dia adalah seorang ahli matematik.\\'\\n\\n### Response:\\n\\'Dia seorang ahli matematik.\\'']"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "49000"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('prepared-alpaca.json', 'w') as fopen:\n",
    "    json.dump(data, fopen)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
